﻿(function () {
    $(function () {
        var _hazardSourceService = abp.services.app.hazardSource;
        var _$modal = $("#RiskFactorEditModal");
        var _$form = _$modal.find("form");
     
        //保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            if (!_$form.valid()) {                
                return;
            }
          
            if ($('#AccidentType option:selected').val() == "") {
                alert("请选择事故类型");
                return;
            }
            if ($('#PostId option:selected').val() == "") {
                alert("请选择岗位");
                return;
            }

            var hazardsource = _$form.serializeFormToObject();
            
            abp.ui.setBusy(_$modal);

            _hazardSourceService.createOrUpdateHazardSource({ hazardsource}).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });



        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost()
        });

        function refreshpost() {
            location.reload();
        }

        //点击编辑按钮，打开编辑模态窗口
        $(".edit-hazardsource").click(function (e) {
            e.preventDefault();
            var hazardsourceId = $(this).attr("data-hazardsource-id");

            $.ajax({
                url: abp.appPath + 'RiskFactor/EditRiskFactorModal?hazardsourceId=' + hazardsourceId,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#RiskFactorEditModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });





        //打开编辑窗口，传递参数HazardSourceId
        //$('#RiskFactorEditModal').on('show.bs.modal', function (event) {
        //    var button = $(event.relatedTarget); // Button that triggered the modal
        //    var hazardsourceId = button.data('hazardsource-id'); // Extract info from data-* attributes
        //    var riskfactor = button.data('hazardsource-riskfactor');
        //    var accidenttype = button.data('hazardsource-accidenttype');
        //    var postid = button.data('hazardsource-postid');
        //    var modal = $(this);
        //    modal.find('#Id').val(hazardsourceId);

        //    modal.find("#RiskFactor").val(riskfactor);

        //    $("#AccidentType").val(accidenttype);
        //    modal.find("#AccidentType").selectpicker('refresh');

        //    $("#PostId").val(postid);
        //    modal.find("#PostId").selectpicker("refresh");
        //});

        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();